package com.shop.manager.controller;

//import com.shop.manager.entity.Provinces;
//import com.shop.manager.http.HttpResult;
//import com.shop.manager.http.PageResult;
import com.base.common.entity.Provinces;
import com.base.common.http.HttpResult;
import com.base.common.http.PageResult;
import com.shop.manager.service.ProvincesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 省份增删改查
 */
@RestController
@RequestMapping("/provinces")
public class ProvincesController {

    /**
     * @Description 注入service对象
     */
    @Autowired
    private ProvincesService provincesService;

    /**
     * @Description 添加省份信息
     * @param provinces 省份对象
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public HttpResult add(@RequestBody(required = false) Provinces provinces){
        try {
            provincesService.add(provinces);
            return HttpResult.ok("添加省份成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("添加省份失败!");
    }

    /**
     * @Description 批量删除省份
     * @param ids 省份id
     * @return
     */
    @RequestMapping("/delete")
    public HttpResult delete(@RequestBody Integer[] ids){
        try {
            provincesService.deleteByIds(ids);
            //删除成功
            return HttpResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //删除异常
            return HttpResult.error();
        }
    }

    /**
     * @Description 修改省份
     * @param provinces 修改的省份对象
     * @return
     */
    @RequestMapping("/update")
    public HttpResult update(@RequestBody(required = false) Provinces provinces){
        try {
            provincesService.update(provinces);
            return HttpResult.ok("修改省份成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("修改省份失败!");
    }

    /**
     * @Description 根据ID查询省份
     * @param id 省份id
     * @return
     */
    @RequestMapping("/getById")
    public HttpResult getById(@RequestParam("id") Integer id){
        return HttpResult.ok(provincesService.findOne(id));
    }

    /**
     * @Description 条件分页查询
     * @param pageNum 当前页码
     * @param size 当前页条数
     * @param provinces 查询条件对象
     * @return
     */
    @RequestMapping("/getAll")
    public HttpResult getAll(@RequestParam("pageNum") Integer pageNum, @RequestParam("size") Integer size, @RequestBody(required = false) Provinces provinces){
        try {
            //分页查询
            PageResult result = provincesService.findPage(pageNum, size, provinces);
            //查询成功
            return HttpResult.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            //查询异常
            return HttpResult.error("查询省份异常");
        }
    }
}
